﻿#region Using Statemens
using System;
using GameBase.Screens;
using GameBase.Sprites;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using StarFox.ScreenComponents;
#endregion

namespace StarFox.Screens
{
    /// <summary>
    /// Gamepad layout screen of the game.
    /// </summary>
    public class GamepadLayoutScreen : StarFoxScreen
    {
        #region Fields

        SpriteSheet spriteSheet;

        Rectangle dstRectGmpImg;
        Rectangle dstRectGmpDsc;

        Texture2D bkgTex;
        Rectangle bkgRect;

        #endregion

        #region Initialization

        /// <summary>
        /// Initializes a new instance of this class.
        /// </summary>
        /// <param name="screenManager">The ScreenManager that this Screen is associated to.</param>
        public GamepadLayoutScreen(ScreenManager screenManager)
            : base(screenManager)
        {
            Name = "GamepadLayoutScreen";
        }

        /// <summary>
        /// Called when the Screen needs to be initialized.
        /// </summary>
        public override void Initialize()
        {
            Components3D.Add(new MenuBackground(this) { Duration = TimeSpan.FromSeconds(6) });
            Components2D.Add(new SimpleMenu(this) { MenuTitle = "TITLE_GAMEPAD_LAYOUT" });

            spriteSheet = Game.Content.Load<SpriteSheet>(ContentSettings.GetAsset("MENU_SPRITES"));

            Initialize_Message();
            Initialize_Background();

            base.Initialize();
        }

        #region Initialize_Message

        private void Initialize_Message()
        {
            Rectangle auxRect;

            // gamepad
            auxRect = spriteSheet.SourceRectangle("GAMEPAD_IMG");
            dstRectGmpImg = new Rectangle(0, 0, (int)(auxRect.Width * Scale), (int)(auxRect.Height * Scale));
            dstRectGmpImg.Location = new Point((int)((Width - dstRectGmpImg.Width) * 0.5f), (int)((0.96f * Height - dstRectGmpImg.Height) * 0.5f));

            // description
            auxRect = spriteSheet.SourceRectangle("GAMEPAD_DSC");
            dstRectGmpDsc = new Rectangle(0, 0, (int)(auxRect.Width * Scale), (int)(auxRect.Height * Scale));
            dstRectGmpDsc.Location = new Point((int)(Width * 0.1225f), (int)(Height * 0.225f));
        }

        #endregion

        #region Initialize_Background

        private void Initialize_Background()
        {
            bkgTex = new Texture2D(GraphicsDevice, 1, 1);
            bkgTex.SetData(new[] { new Color(51, 51, 51, 191) });
            bkgRect = new Rectangle(0, (int)(Height * 0.19f), Width, (int)(Height * 0.52f));
        }

        #endregion

        #endregion

        #region Dispose

        // dispose flag
        bool isDisposed = false;

        /// <summary>
        /// Releases the unmanaged resources used by this object and optionally releases the managed resources.
        /// </summary>
        /// <param name="disposing">true to release both managed and unmanaged resources; false to release only unmanaged resources.</param>
        protected override void Dispose(bool disposing)
        {
            if (!isDisposed)
            {
                if (disposing)
                {
                    if (bkgTex != null) { bkgTex.Dispose(); }

                    base.Dispose(disposing);
                }

                isDisposed = true;
            }
        }

        #endregion

        #region Draw

        /// <summary>
        /// Called when 2D content of the Screen needs to be drawn.
        /// </summary>
        /// <param name="gameTime">Time elapsed since the last call to this method.</param>
        protected override void Draw2D(GameTime gameTime)
        {
            base.Draw2D(gameTime);

            SpriteBatch.Begin();

            // background
            SpriteBatch.Draw(bkgTex, bkgRect, Color.Yellow);

            // gamepad
            SpriteBatch.Draw(spriteSheet.Texture, dstRectGmpImg, spriteSheet.SourceRectangle("GAMEPAD_IMG"), Color.Gold);

            // description
            SpriteBatch.Draw(spriteSheet.Texture, dstRectGmpDsc, spriteSheet.SourceRectangle("GAMEPAD_DSC"), Color.Red);

            SpriteBatch.End();
        }

        #endregion
    }
}
